package tech.aistar.controller.demand;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.*;
import tech.aistar.service.demand.IDemandService;
import tech.aistar.service.demand.impl.CategoryDetailServiceImpl;
import tech.aistar.service.demand.impl.CategoryServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by 豆梦寒 on 2019/10/3.
 */
@Controller
@RequestMapping("/demand")
public class AddDemandController {
    @Autowired
    private IDemandService iDemandService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private CategoryDetailServiceImpl categoryDetailService;



    /**
     * 获得所有类别的详细信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("getCategoryDetail")
    public List<CategoryDetail> getCategoryDetail(Integer id){
        List<CategoryDetail> categoryDetailList = categoryDetailService.findAll(id);
        return categoryDetailList;
    }

    /**
     * 类别
     */
    @GetMapping("/demandRelease")
    public String demandReleaseView(Model model){
        List<Category> categoryList =  categoryService.findAll();
        model.addAttribute("categoryList",categoryList);

        return "demand/demandRelease";
    }

    @RequestMapping("/demandReleaseView")
    @ResponseBody
    public Result Release(@RequestBody Demand demand, HttpSession session){


        System.out.println(demand);
        iDemandService.add(demand);
        return new Result(CodeStatus.OK,true,"发布成功!",demand);

    }



}
